<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/4 0004
 * Time: 15:25
 */

namespace app\currency\controller;


use app\currency\service\AgentService;
use library\Controller;
use think\Db;

/**
 * 代理商管理
 * Class Agent
 * @package app\currency\controller
 */
class Agent extends Controller
{
    protected $table = 'Agent';

    /**
     * 代理商列表
     * @menu true
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '代理商列表';
        $this->_query($this->table)
            ->like('name,phone')
            ->equal('status')
            ->dateBetween('create_at')
            ->order('id desc')
            ->page();
    }

    /**
     * 添加代理商
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->_form($this->table,'form');
    }

    /**
     * 编辑代理商
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->_form($this->table,'form');
    }
    /**
     * 表单数据处理
     * @param array $data
     */
    public function _form_filter(&$data)
    {
        if ($this->request->isPost()) {
            if (isset($data['id'])) {
                unset($data['phone']);
            } elseif (Db::name($this->table)->where(['phone' => $data['phone']])->count() > 0) {
                $this->error('手机号已经存在，请使用其它手机号！');
            }
        }
    }
    /**
     * 禁用
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->applyCsrfToken();
        $this->_save($this->table,['status' => '0']);
    }

    /**
     * 启用
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->applyCsrfToken();
        $this->_save($this->table,['status' => '1']);
    }

    /**
     * 删除
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->applyCsrfToken();
        $this->_delete($this->table);
    }
    public function rebate(){
        $this->title = '收益记录';
        $id = $this->request->get('id');
        $where = [['agent_id','eq',$id]];
        $this->_query('AgentRebate')->where($where)->order('create_at desc')->page();
    }
    protected function _rebate_page_filter(array &$data)
    {
        $cids = array_unique(array_column($data, 'cid'));
        $cleaner_list = Db::name('Cleaner')->whereIn('id', $cids)->select();
        foreach ($data as &$datum) {
            $datum['cleaner'] = [];
            foreach ($cleaner_list as $cleaner) if($cleaner['id'] === $datum['cid']) {
                $datum['cleaner'] = $cleaner;
            }
            $datum['modal_txt'] = $datum['modal'] == 'service' ? '做工': '绑定';
        }
//        dump($data);
    }
    public function team()
    {
        $this->title = '团队列表';
        $id = $this->request->get('id');
        $ids = AgentService::getTeamIds($id);
        $where = [['status','eq','1'],['id','in',$ids]];
        $this->_query('Cleaner')
            ->where($where)
            ->like('name')
            ->page();
    }
}